import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";

const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
    meta: {
      showTabBar: true, // 控制TabBar的显示和隐藏
      auth:false,
    },
  },
  {
    path: "/Course",
    name: "Course",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/CourseView.vue"),
    meta: {
      showTabBar: true, // 控制TabBar的显示和隐藏
      auth:true,
    },
  },
  {
    path: "/Study",
    name: "Study",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/StudyView.vue"),
    meta: {
      showTabBar: true, // 控制TabBar的显示和隐藏
    },
  },
  {
    path: "/My",
    name: "My",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/MyView.vue"),
    meta: {
      showTabBar: true, // 控制TabBar的显示和隐藏
      auth:true,
    },
  },
  // 轮播图详情页
  {
    path: "/swiperInfo",
    name: "swiperInfo",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/swiperInfo.vue"),
    meta: {
      showTabBar: false, // 控制TabBar的显示和隐藏
    },
  },

  // 三个 {更多}详情页
  {
    path: "/CourseInfo",
    name: "CourseInfo",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/CourseInfo.vue"),
    meta: {
      showTabBar: false, // 控制TabBar的显示和隐藏
    },
  },

  //  遍历的列表内容详情信息
  {
    path: "/DetailsCourse",
    name: "DetailsCourse",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/DetailsCourse.vue"),
    meta: {
      showTabBar: false, // 控制TabBar的显示和隐藏
    },
  },

  // 公开课{更多}详情页
  {
    path: "/OpenInfo",
    name: "OpenInfo",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/OpenInfo.vue"),
    meta: {
      showTabBar: false,
    },
  },
  // 文章分享{更多}详情页ArticleInfo
  {
    path: "/ArticleInfo",
    name: "ArticleInfo",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/ArticleInfo.vue"),
    meta: {
      showTabBar: false,
    },
  },

  // 跳转初始登录页面
  {
    path: "/LoginPass",
    name: "LoginPass",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/LoginPass.vue"),
  },
  // 登录成功
  {
    path: "/LoginSuccess",
    name: "LoginSuccess",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/LoginSuccess.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

// 导航守卫
// 所有路由跳转之前都会经过这个函数，你可以在这个函数内部判断能否访问这个路由。起到路由拦截的作用。
router.beforeEach((to, from, next) => {
  if (to.meta.auth) {
    const token = localStorage.getItem("token") || undefined;
    if (token) next();
    else next(`/LoginSuccess?redirect=${to.path}`);
  } else {
    next();
  }
});

export default router;
